package com.panshi.controller;

import com.panshi.api.api.AnswerHDPort;
import com.panshi.api.bean.AnswerHDBean;
import com.panshi.api.util.PageSizeVO;
import com.panshi.pojo.AnswerHDVO;
import com.panshi.pojo.TimeVO;
import com.panshi.servers.AnswerHDServers;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @author XiaoWangYa
 * @version 1.0
 * @date 2020/8/9 0009 下午 10:15
 */
@Slf4j
@Controller
@Api("回答Controller层")
public class AnswerHDController implements AnswerHDPort {

    @Autowired
    private AnswerHDServers answerHDServers;

    public String addAnswerHDInfo(@RequestBody AnswerHDBean answerHDBean) {
        log.info("新增回答数据 --> " + answerHDBean);
        answerHDServers.addAnswerHDInfo(answerHDBean);
        return "ok";
    }

    public String deleteAnswerHDInfo (@PathVariable int id) {
        log.info("删除回答数据 --> " + id);
        answerHDServers.deleteAnswerHDInfo(id);
        return "ok";
    }

    public PageSizeVO queryAssignHDQuestionAnswerInfo(@RequestBody PageSizeVO pageSizeHDBean) {
        log.info("查询指定问答下回答 and 指定用户回答数据" + pageSizeHDBean);
        // 设置分页起始页
        pageSizeHDBean.setStartIndex(pageSizeHDBean.getStartIndexInfo());
        List<AnswerHDVO> list = answerHDServers.queryAssignHDQuestionAnswerInfo(pageSizeHDBean);
        for (AnswerHDVO answerHDVO : list) {
            // 时间转换
            answerHDVO.setDateTime(TimeVO.getTimeFormatText(answerHDVO.getCreated_time()));
        }
        // 返回数据
        PageSizeVO page = new PageSizeVO();
        page.setData(list);
        return page;
    }

    public String updateAnswerHDInfo(@RequestBody AnswerHDBean answerHDBean) {
        log.info("修改回答 --> " + answerHDBean);
        answerHDServers.updateAnswerHDInfo(answerHDBean);
        return "ok";
    }

}
